package com.zhou.JavaAndroidFirm.fragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.activity.NewsChannelActivity;
import com.zhou.JavaAndroidFirm.adapter.BaseFragmentAdapter;
import com.zhou.JavaAndroidFirm.app.AppConstant;
import com.zhou.JavaAndroidFirm.base.BaseFragment;
import com.zhou.JavaAndroidFirm.bean.NewsChannelTable;
import com.zhou.JavaAndroidFirm.constract.NewsMainConstract;
import com.zhou.JavaAndroidFirm.event.ChannelEvent;
import com.zhou.JavaAndroidFirm.event.ScollEvent;
import com.zhou.JavaAndroidFirm.model.NewsMainModel;
import com.zhou.JavaAndroidFirm.presenter.NewsMainPresenter;
import com.zhou.JavaAndroidFirm.utils.MyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2018/7/15 0015
 * @des
 */
public class NewsMainFragment extends BaseFragment<NewsMainModel, NewsMainPresenter> implements NewsMainConstract.View {
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.add_channel_iv)
    ImageView addChannelIv;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    private BaseFragmentAdapter fragmentAdapter;

    @Subscribe
    public void handleEvent(ChannelEvent event) {
        if (event != null) {
            returnMineNewsChannels(event.getTables());
        }
    }

    @Override
    protected void lazyFetchData() {
        mPresenter.lodeMineChannelsRequest();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.app_bar_news;
    }

    @Override
    protected void initPresenter() {
        mPresenter.onCreate(mModel, this);
    }

    @Override
    protected void initView(Bundle bundle) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void initEvent() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void returnMineNewsChannels(List<NewsChannelTable> newsChannelsMine) {
        if (newsChannelsMine != null) {
            List<String> channelNames = new ArrayList<>();
            List<Fragment> mFragements = new ArrayList<>();
            for (int i = 0; i < newsChannelsMine.size(); i++) {
                channelNames.add(newsChannelsMine.get(i).getNewsChannelName());
                mFragements.add(createListFragment(newsChannelsMine.get(i)));
            }
            if (null == fragmentAdapter) {
                fragmentAdapter = new BaseFragmentAdapter(getChildFragmentManager(), mFragements, channelNames);
            } else {
                fragmentAdapter.setFragments(getChildFragmentManager(), mFragements, channelNames);
            }
            viewPager.setAdapter(fragmentAdapter);
            tabs.setupWithViewPager(viewPager);
            MyUtils.dynamicSetTabLayoutMode(tabs);
            initEvent();
        }
    }

    private Fragment createListFragment(NewsChannelTable bean) {
        NewsFragment newsFragment = new NewsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.NEWS_ID, bean.getNewsChannelId());
        bundle.putString(AppConstant.NEWS_TYPE, bean.getNewsChannelType());
        bundle.putInt(AppConstant.CHANNEL_POSITION, bean.getNewsChannelIndex());
        newsFragment.setArguments(bundle);
        return newsFragment;
    }

    @Override
    public void showTip(String tip) {

    }

    @OnClick({R.id.add_channel_iv, R.id.fab})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_channel_iv:
                NewsChannelActivity.startAction(mContext);
                break;
            case R.id.fab:
                EventBus.getDefault().post(new ScollEvent());
                break;
            default:
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
